package com.acwing.partition11;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/3/3 8:40
 */
public class AC1055股票买卖II {

    private static final int N = 100010;
    private static int[] nums = new int[N];
    private static int n;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        String[] ss = reader.readLine().split(" ");
        for (int i = 0; i < n; i++) nums[i] = Integer.parseInt(ss[i]);
        int ans = 0;
        for (int i = 0; i < n - 1; i++) {
            if (nums[i + 1] > nums[i]) ans += nums[i + 1] - nums[i];
        }
        writer.write(ans + "\n");
        writer.flush();
    }
}
